package com.example.forum.mapper;


import com.example.forum.entity.Favo;
import com.example.forum.entity.User;
import com.example.forum.entity.Visitor;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface UserMapper {
    //查询账户是否存在，返回null即可创建新用户
    //2.查询用户对应的信息，从而获取id 密码
    @Select("select * from user where username=#{username}")
    User selectUser(@Param("username")String username);
    @Select("select * from user where id=#{id}")
    User selectUserByid(@Param("id")Integer id);

    @Select("select username,avatar from user order by update_at desc limit 5")
    List<Visitor> visitor();

    //新增用户
    @Insert("insert into user(nickname,username,password,avatar)values(#{user.nickname},#{user.username},#{user.password},#{user.avatar})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(@Param("user") User user);

    //根据userID查username，生成上下文用户
    @Select("select username from user where id=#{id}")
    String username(@Param("id")Integer id);

    //@Update("update user set ")更改昵称密码

    @Update("<script>"+
        "update user set"+
        "<if test='user.mobile!=null'>mobile=#{user.mobile},</if>"+
            "<if test='user.nickname!=null'>nickname=#{user.nickname},</if>"+
            "<if test='user.password!=null'>password=#{user.password},</if>"+
            "<if test='user.avatar!=null'>avatar=#{user.avatar},</if>"+
            "username=#{username}"+
            "where username=#{username}"+
            "</script>"
    )
    int update(@Param("username")String username,@Param("user")User user);

    // 查看当前文章有没有收藏，没有就新增，有就update状态
    @Select("select * from favo where user_id=#{userId} and article_id=#{articleId}")
    Favo favoSet(@Param("userId")Integer userId,@Param("articleId")Integer articleId);
//新增
    @Insert("insert into favo(user_id,article_id,favorite,love)values(#{favo.userId},#{favo.articleId},#{favo.favorite},#{favo.love})")
    Integer favoInsert(@Param("favo") Favo favo);

    @Update("update favo set favorite=#{favo.favorite} where user_id=#{favo.userId} and article_id=#{favo.articleId}")
    Integer favoUpdate(@Param("favo")Favo favo);

    @Update("update favo set love=#{favo.love} where user_id=#{favo.userId} and article_id=#{favo.articleId}")
    Integer loveUpdate(@Param("favo")Favo favo);


    //登录时间更新
    @Update("update user set update_at=#{date} where username=#{username}")
    Integer loginUpdate(@Param("date") Date date,@Param("username")String username);
    //查找
    @Select("select * from favo where user_id=#{userId} and article_id=#{articleId} order by id desc")
    Favo selectFavo(@Param("userId")int userId,@Param("articleId")int articleId);

    @Select("select article_id from favo where user_id=#{userId} and favorite=true order by id desc")
    List<Integer> selectArticleId(@Param("userId")int userId);


}
